/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import ReadXML.ImageInfo;
import java.util.Random;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class DrawImage extends Canvas implements Runnable {

    private Image sample, nomalThumb, hoverThumb, failThumb;
    private Image bufferImage, live;
    private int x = 1, y = 1;
    protected int question = 0;
    private Start midlet;
    protected int line = 1, aline = 1;
    protected boolean isMenu = false, isAnswer = false, goiY, saveScore;
    private boolean one = false, two = false, three = false, four = false;
    private boolean oneFailed = false, twoFailed = false, threeFailed = false, fourFailed = false;
    private boolean oneOpen = true, twoOpen = true, threeOpen = true, fourOpen = true;
    private String aof1, aof2;
    private Random imageRandom;
    private boolean next = false, running = true;
    public static boolean wait = false;
    private ImageInfo imageInfo;
    private int i;
    private Thread t;
    private int failed, instanceKeyCode;
    private Font font;
    protected q1 q1;
    private HuongDan1 huongDan1;

    public DrawImage(Start midlet, ImageInfo imageInfo) {
        this.setFullScreenMode(true);
        this.midlet = midlet;
        this.live = DrawChoice.live;
        this.imageInfo = imageInfo;
        DrawChoice.demBai++;
        DrawChoice.level = 1;

        bufferImage = Image.createImage(getWidth(), getHeight());

        Runtime rt = Runtime.getRuntime();
        System.gc();
        System.out.println("Before = " + rt.freeMemory());

        imageRandom = new Random();
        i = (imageRandom.nextInt() << 6) % 3;
        if (i < 0) {
            i = -i;
        }
        if (DrawChoice.demBai == 2) {
            DrawTrialLevel.imgID++;
            i = DrawTrialLevel.imgID + 4;
        } else {
            DrawTrialLevel.imgID = i;
            i = i + 4;
        }

        q1 = new q1(this, imageInfo);
        PassTextboxText.setTextBoxText("");

        huongDan1 = new HuongDan1(midlet, imageInfo.getVsuggest(i));
        sample = PictureList.getImage(i);
        aof1 = imageInfo.getVresultText(i);
        aof2 = imageInfo.getVresultSign(i);

        if (sample.getHeight() > getHeight() / 2) {
            sample = ImageScaled.CreateScaledImage(sample, getHeight() / 2, getHeight() / 2);
        }

        int width = sample.getWidth() / 2;
        int height = sample.getHeight() / 2;
        nomalThumb = ImageScaled.CreateScaledImage(DrawChoice.q_nomal, width, height);
        hoverThumb = ImageScaled.CreateScaledImage(DrawChoice.q_hover, width, height);
        failThumb = ImageScaled.CreateScaledImage(DrawChoice.q_failed, width, height);

        imageInfo = null;
        System.gc();
        System.out.println("After = " + rt.freeMemory());

        t = new Thread(this);
        t.start();
    }

    public void run() {
        long point = 0;
        int i = 1, j = 1;
        boolean timeout = false, setPoint = true;
        while (running) {

            if (question == 0) {
                i = 1;
                setPoint = true;
            } else {
                if (setPoint) {
                    point = System.currentTimeMillis();
                    setPoint = false;
                }
                if (System.currentTimeMillis() - point >= 1000 * i) {
                    if (DrawTrialLevel.time > 0) {
                        DrawTrialLevel.time--;
                        j = 1;
                        repaint();

                    } else {
                        timeout = true;
                    }
                    i++;
                }
                if (timeout == true && j == 1 && DrawTrialLevel.time == 0) {
                    Alert fail = new Alert("", "Hết thời gian", null, AlertType.INFO);
                    fail.setTimeout(1000);
                    midlet.getDis().setCurrent(fail, this);
                    j = 2;
                    timeout = false;
                    if (DrawChoice.level == 1) {
                        if (x == 1 && y == 1) {
                            one = true;
                            oneOpen = false;
                            oneFailed = true;
                            failed++;
                        }
                        if (x == 2 && y == 1) {
                            two = true;
                            twoOpen = false;
                            twoFailed = true;
                            failed++;
                        }
                        if (x == 1 && y == 2) {
                            three = true;
                            threeOpen = false;
                            threeFailed = true;
                            failed++;
                        }
                        if (x == 2 && y == 2) {
                            four = true;
                            fourOpen = false;
                            fourFailed = true;
                            failed++;
                        }
                        isMenu = false;
                        question = 0;
                        q1.aline = 1;
                        repaint();
                    }
                }
            }
        }
        System.gc();
    }

    protected void paint(Graphics g) {

        if (DrawChoice.pass) {
            q1.aline = 0;
            checkAnswer();
        } else {
            String answer = PassTextboxText.getTextBoxText();
            if (DrawChoice.bigAnswer) {
                if (answer.equalsIgnoreCase(aof1) || answer.equalsIgnoreCase(aof2)) {
                    DrawChoice.score += 20;
                    PassTextboxText.setTextBoxText("");
                    DrawChoice.level++;
                    next = true;
                    running = false;
                    victory();
                } else {
                    Alert fail = new Alert("", "Chưa chính xác", null, AlertType.INFO);
                    fail.setTimeout(1000);
                    midlet.getDis().setCurrent(fail);
                    DrawChoice.liv--;
                    PassTextboxText.setTextBoxText("");
                    if (DrawChoice.liv == 0) {
                        Alert gameOver = new Alert("Game Over", "Bạn đã thua !", null, AlertType.INFO);
                        gameOver.setTimeout(Alert.FOREVER);
                        midlet.getDis().setCurrent(gameOver, new ScoreSave(midlet, true, DrawChoice.score));
                    }
                }
                DrawChoice.bigAnswer = false;
            }
            if (!next) {
                if (failed == 4) {
                    Alert gameOver = new Alert("Game Over", "Bạn đã thua !", null, AlertType.INFO);
                    gameOver.setTimeout(Alert.FOREVER);
                    midlet.getDis().setCurrent(gameOver, new ScoreSave(midlet, true, DrawChoice.score));
                } else {
                    renderWorld();
                }
            }

            g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
        }
    }

    private void victory() {
        if (DrawChoice.demBai != 2) {
            Alert fail = new Alert("Chúc mừng !", "Bạn đã vượt qua Bài " + DrawChoice.demBai + ".Nhấn "
                    + "Tiếp tục để sang Bài tiếp theo", null, AlertType.INFO);
            fail.setTimeout(Alert.FOREVER);
            midlet.getDis().setCurrent(fail);
        } else {
            Alert fail = new Alert("Chúc mừng !", "Bạn đã vượt qua màn Khởi động. Nhấn tiếp tục để sang màn Tăng tốc", null, AlertType.INFO);
            fail.setTimeout(Alert.FOREVER);
            midlet.getDis().setCurrent(fail);
        }

        Graphics g = bufferImage.getGraphics();
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(font);
        g.setColor(0x00000000);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x00EAEE0A);
        g.fillRect(5, 5, getWidth() - 10, getHeight() - font.getHeight() - 12);

        g.setColor(0x00DD6F00);
        g.fillRect(0, getHeight() - font.getHeight() - 2, getWidth(), font.getHeight() + 2);

        g.setColor(0xffffff);
        g.drawLine(0, getHeight() - font.getHeight() - 2, getWidth(), getHeight() - font.getHeight() - 2);
        g.drawString("Tiếp tục", getWidth() - 1, getHeight() - 1, Graphics.RIGHT | Graphics.BOTTOM);
        g.drawString("Thoát", 1, getHeight() - 1, Graphics.LEFT | Graphics.BOTTOM);
        g.setColor(0x000000);
        g.drawString("Congratulation !", getWidth() / 2, 10, Graphics.HCENTER | Graphics.TOP);
        g.drawImage(sample, getWidth() / 2, 15 + font.getHeight(), Graphics.HCENTER | Graphics.TOP);
        g.setColor(255, 0, 0);
        g.drawString("Điểm số của bạn " + String.valueOf(DrawChoice.score), getWidth() / 2, 20 + font.getHeight() + sample.getHeight(), Graphics.HCENTER | Graphics.TOP);
    }

    private void renderWorld() {

        Graphics g = bufferImage.getGraphics();
        g.setColor(0x00f0dfb6);
        g.fillRect(0, 0, getWidth(), getHeight());

        if (question == 0) {

            g.setColor(0x00f8efdb);
            Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
            g.setFont(f);
            g.fillRoundRect((getWidth() - sample.getWidth()) / 2, (getHeight() - sample.getHeight()) / 2 - 25 - f.getHeight(),
                    sample.getWidth() / 2 - 3, f.getHeight() + 10, 5, 5);
            g.fillRoundRect(getWidth() / 2 + 3, (getHeight() - sample.getHeight()) / 2 - 25 - f.getHeight(),
                    sample.getWidth() / 2 - 3, f.getHeight() + 10, 5, 5);
            g.setColor(0x000000);
            g.drawString("BÀI " + DrawChoice.demBai, getWidth() / 2 - sample.getWidth() / 4, (getHeight() - sample.getHeight()) / 2 - 22,
                    Graphics.HCENTER | Graphics.BASELINE);
            g.drawString(String.valueOf(DrawChoice.score), getWidth() / 2 + sample.getWidth() / 4, (getHeight() - sample.getHeight()) / 2 - 22,
                    Graphics.HCENTER | Graphics.BASELINE);

            g.setColor(0x00f6bd35);
            g.fillRoundRect((getWidth() - sample.getWidth()) / 2 - 10, (getHeight() - sample.getHeight()) / 2 - 10,
                    sample.getWidth() + 20, sample.getHeight() + 20, 5, 5);
//            draw Menu
            g.setColor(0x00b0711f);
            font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
            g.setFont(font);
            g.fillRect(0, getHeight() - font.getHeight() - 4, getWidth(), font.getHeight() + 4);
            g.setColor(0x00ffffff);
            g.drawLine(0, getHeight() - font.getHeight() - 4, getWidth(), getHeight() - font.getHeight() - 4);
            g.drawString("Menu", 1, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);
            if (one && two && three && four && !isMenu) {
                g.drawString("Bỏ qua", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
            } else {
                g.drawString("Chọn", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
            }

            if (DrawChoice.liv == 3) {
                g.drawImage(live, getWidth() / 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
                g.drawImage(live, getWidth() / 2 - live.getWidth() - 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
                g.drawImage(live, getWidth() / 2 + live.getWidth() + 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
            } else if (DrawChoice.liv == 2) {
                g.drawImage(live, getWidth() / 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
                g.drawImage(live, getWidth() / 2 - live.getWidth() - 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
            } else if (DrawChoice.liv == 1) {
                g.drawImage(live, getWidth() / 2 - live.getWidth() - 2, (getHeight() + sample.getHeight()) / 2 + 11,
                        Graphics.HCENTER | Graphics.TOP);
            }

            g.drawImage(sample, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);

            if (!one) {
                //                g.fillRect((getWidth() - sample.getWidth()) / 2, (getHeight() - sample.getHeight()) / 2,
                //                        sample.getWidth() / 2, sample.getHeight() / 2);

                g.drawImage(nomalThumb, (getWidth() - sample.getWidth()) / 2,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);

            }

            if (oneFailed) {
                g.drawImage(failThumb, (getWidth() - sample.getWidth()) / 2,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }

            g.setColor(0x00DDB19C);
            if (x == 1 && y == 1) {
                if (!one) {
                    g.drawImage(hoverThumb, (getWidth() - sample.getWidth()) / 2,
                            (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);

                }
                g.setColor(0x00ED3838);
                g.drawRect((getWidth() - sample.getWidth()) / 2 + 1, (getHeight() - sample.getHeight()) / 2 + 1,
                        sample.getWidth() / 2 - 2, sample.getHeight() / 2 - 2);
                g.setColor(0x00D17B53);
            }


            if (!two) {
//                g.fillRect(getWidth() / 2, (getHeight() - sample.getHeight()) / 2,
//                        sample.getWidth() / 2, sample.getHeight() / 2);
                g.drawImage(nomalThumb, getWidth() / 2,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }
            if (twoFailed) {
                g.drawImage(failThumb, getWidth() / 2,
                        (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 2 && y == 1) {
                if (!two) {
                    g.drawImage(hoverThumb, getWidth() / 2,
                            (getHeight() - sample.getHeight()) / 2, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 + 1, (getHeight() - sample.getHeight()) / 2 + 1,
                        sample.getWidth() / 2 - 2, sample.getHeight() / 2 - 2);
                g.setColor(0x00D17B53);
            }

            if (!three) {
                g.drawImage(nomalThumb, (getWidth() - sample.getWidth()) / 2,
                        getHeight() / 2, Graphics.LEFT | Graphics.TOP);
            }
            if (threeFailed) {
                g.drawImage(failThumb, (getWidth() - sample.getWidth()) / 2,
                        getHeight() / 2, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 1 && y == 2) {
                if (!three) {
                    g.drawImage(hoverThumb, (getWidth() - sample.getWidth()) / 2,
                            getHeight() / 2, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect((getWidth() - sample.getWidth()) / 2 + 1, getHeight() / 2 + 1,
                        sample.getWidth() / 2 - 2, sample.getHeight() / 2 - 2);
                g.setColor(0x00D17B53);
            }

            if (!four) {
                g.drawImage(nomalThumb, getWidth() / 2, getHeight() / 2, Graphics.LEFT | Graphics.TOP);
            }
            if (fourFailed) {
                g.drawImage(failThumb, getWidth() / 2, getHeight() / 2, Graphics.LEFT | Graphics.TOP);
            }
            g.setColor(0x00DDB19C);
            if (x == 2 && y == 2) {
                if (!four) {
                    g.drawImage(hoverThumb, getWidth() / 2, getHeight() / 2, Graphics.LEFT | Graphics.TOP);
                }
                g.setColor(0x00ED3838);
                g.drawRect(getWidth() / 2 + 1, getHeight() / 2 + 1,
                        sample.getWidth() / 2 - 2, sample.getHeight() / 2 - 2);
                g.setColor(0x00D17B53);
            }

            String s1 = "Giải hình lớn";
            String s2 = "Gợi ý";
            String s3 = "Lưu điểm";
            String s4 = "Thoát";
            if (isMenu) {
                //draw Menu
                font = Font.getDefaultFont();
                g.setColor(0x00000000);
                g.drawRect(0, getHeight() - 5 * font.getHeight() - 12, font.stringWidth(s1) + 10, 4 * font.getHeight() + 10);
                g.setColor(0x00F0E001);
                g.fillRect(0 + 1, getHeight() - 5 * font.getHeight() - 12 + 1, font.stringWidth(s1) + 8, 4 * font.getHeight() + 8);

                if (line == 1) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 5 * font.getHeight() - 12 + 1, font.stringWidth(s1) + 8, font.getHeight() + 2);
                }
                if (line == 2) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 4 * font.getHeight() - 10 + 1, font.stringWidth(s1) + 8, font.getHeight() + 2);
                }
                if (line == 3) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 3 * font.getHeight() - 8 + 1, font.stringWidth(s1) + 8, font.getHeight() + 2);
                }
                if (line == 4) {
                    g.setColor(0x0041F001);
                    g.fillRect(0 + 1, getHeight() - 2 * font.getHeight() - 6 + 1, font.stringWidth(s1) + 9, font.getHeight() + 3);
                }

                g.setFont(font);
                g.setColor(0x00000000);
                g.drawString(s4, 0 + 2, getHeight() - font.getHeight() - 3 - 2, Graphics.LEFT | Graphics.BASELINE);
                g.drawString(s3, 0 + 2, getHeight() - 2 * font.getHeight() - 3 - 2 * 2, Graphics.LEFT | Graphics.BASELINE);
                g.drawString(s2, 0 + 2, getHeight() - 3 * font.getHeight() - 3 - 2 * 3, Graphics.LEFT | Graphics.BASELINE);
                g.drawString(s1, 0 + 2, getHeight() - 4 * font.getHeight() - 3 - 2 * 4, Graphics.LEFT | Graphics.BASELINE);

            }
        } else {
            q1.render(g);

        }
    }

    private void checkAnswer() {
        if (q1.aline == q1.getAnswer() && DrawTrialLevel.time > 0) {
            if (x == 1 && y == 1) {
                one = true;
                oneOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 2 && y == 1) {
                two = true;
                twoOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 1 && y == 2) {
                three = true;
                threeOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }
            if (x == 2 && y == 2) {
                four = true;
                fourOpen = false;

                if (goiY) {
                    DrawChoice.score += 5;
                    goiY = false;
                } else {
                    DrawChoice.score += 10;
                }
            }


            isMenu = false;
            question = 0;
            q1.aline = 1;

            midlet.disManager.pushDisplayable(this);
            DrawTrialLevel.HD2.createHD2(q1.getMean(), DrawChoice.score);
            midlet.getDis().setCurrent(DrawTrialLevel.HD2);

            repaint();
        } else {

            if (!DrawChoice.pass) {
                Alert fail = new Alert("", "Chưa chính xác", null, AlertType.INFO);
                fail.setTimeout(1000);
                midlet.getDis().setCurrent(fail, this);
            } else {
                DrawChoice.pass = false;
            }

            if (x == 1 && y == 1) {
                one = true;
                oneOpen = false;
                oneFailed = true;
                failed++;
            }
            if (x == 2 && y == 1) {
                two = true;
                twoOpen = false;
                twoFailed = true;
                failed++;
            }
            if (x == 1 && y == 2) {
                three = true;
                threeOpen = false;
                threeFailed = true;
                failed++;
            }
            if (x == 2 && y == 2) {
                four = true;
                fourOpen = false;
                fourFailed = true;
                failed++;
            }
            isMenu = false;
            question = 0;
            q1.aline = 1;
            repaint();
        }
    }

    protected void pointerPressed(int x, int y) {
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);

        if (question == 0) {
            if (!isMenu) {
                if (x >= (getWidth() - sample.getWidth()) / 2 && x <= getWidth() / 2) {
                    if (y <= getHeight() / 2 && y >= (getHeight() - sample.getHeight()) / 2) {
                        this.x = this.y = 1;
                        if (oneOpen) {
                            question = 1;
                            q1.createQuestion(60);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 && y <= (getHeight() + sample.getHeight()) / 2) {
                        this.x = 1;
                        this.y = 2;
                        if (threeOpen) {
                            question = 1;
                            q1.createQuestion(60);
                            repaint();
                        }
                    }
                }
                if (x <= (getWidth() + sample.getWidth()) / 2 && x >= getWidth() / 2) {
                    if (y <= getHeight() / 2 && y >= (getHeight() - sample.getHeight()) / 2) {
                        this.x = 2;
                        this.y = 1;
                        if (twoOpen) {
                            question = 1;
                            q1.createQuestion(60);
                            repaint();
                        }
                    } else if (y >= getHeight() / 2 && y <= (getHeight() + sample.getHeight()) / 2) {
                        this.x = 2;
                        this.y = 2;
                        if (fourOpen) {
                            question = 1;
                            q1.createQuestion(60);
                            repaint();
                        }
                    }
                }
            } else {
                font = Font.getDefaultFont();
                if (x >= 0 && x <= font.stringWidth("Giải hình lớn") + 20) {
                    if (y >= getHeight() - 5 * font.getHeight() - 12 + 1 && y < getHeight() - 4 * font.getHeight() - 10 + 1) {
                        if ((one & !oneFailed) || (two & !twoFailed) || (three & !threeFailed) || (four & !fourFailed)) {
                            isMenu = false;
                            line = 1;
                            DrawChoice.bigAnswer = true;
                            question = 0;
                            midlet.disManager.pushDisplayable(this);
                            InsertAnswer ia = new InsertAnswer(midlet);
                            midlet.disManager.pushDisplayable(ia.insertAnswer("Giải hình lớn"));
                        }
                    } else if (y >= getHeight() - 4 * font.getHeight() - 10 + 1 && y < getHeight() - 3 * font.getHeight() - 8 + 1) {
                        isMenu = false;
                        line = 1;
                        question = 0;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(huongDan1);
                    } else if (y >= getHeight() - 3 * font.getHeight() - 8 + 1 && y < getHeight() - 2 * font.getHeight() - 6 + 1) {
                        isMenu = false;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new ScoreSave(midlet, false, DrawChoice.score));
                    } else if (y >= getHeight() - 2 * font.getHeight() - 6 + 1 && y < getHeight() - font.getHeight() - 1) {
                        DrawChoice.score = 0;
                        DrawChoice.liv = 3;
                        DrawChoice.demBai = 0;
                        huongDan1 = null;
                        System.gc();

                        midlet.disManager.Home();
                    }
                }
            }
        } else {
            if (x >= 0 && x <= font.stringWidth("Hướng dẫn") + 10) {
                if (q1.isMenu) {
                    if (y >= getHeight() - 3 * font.getHeight() - 10 && y < getHeight() - 2 * font.getHeight() - 8) {
                        goiY = true;
                        q1.isMenu = false;
                        Alert goiy = new Alert("Gợi ý", (String) imageInfo.getVsuggest(q1.i),
                                null, AlertType.INFO);
                        goiy.setTimeout(Alert.FOREVER);
                        midlet.getDis().setCurrent(goiy, this);
                    } else if (y >= getHeight() - 2 * font.getHeight() - 8 && y < getHeight() - font.getHeight()) {
                        q1.isMenu = false;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(DrawChoice.alertCon);
                    }
                }
            } else if (x <= getWidth() && x >= getWidth() - q1.lengthString() - 15) {
                font = Font.getDefaultFont();
                if (q1.isAnswer) {
                    if (y >= getHeight() - 5 * font.getHeight() - 5 * 4 - 1 && y < getHeight() - 4 * font.getHeight() - 4 * 4) {
                        q1.aline = 1;
                        checkAnswer();
                    } else if (y >= getHeight() - 4 * font.getHeight() - 4 * 4 && y < getHeight() - 3 * font.getHeight() - 3 * 4 + 1) {
                        q1.aline = 2;
                        checkAnswer();
                    } else if (y >= getHeight() - 3 * font.getHeight() - 3 * 4 + 1 && y < getHeight() - 2 * font.getHeight() - 2 * 4 + 2) {
                        q1.aline = 3;
                        checkAnswer();
                    } else if (y >= getHeight() - 2 * font.getHeight() - 2 * 4 + 2 && y < getHeight() - font.getHeight() - 1) {
                        q1.aline = 4;
                        checkAnswer();
                    }
                }
            }
        }
        if (y <= getHeight() && y >= getHeight() - font.getHeight() - 10) {
            if (x <= getWidth() && x >= getWidth() - font.stringWidth("Chọn đáp án") - 10) {
                softKeyRightFunction();
            }
            if (x >= 0 && x <= font.stringWidth("Menu") + 20) {
                softKeyLeftFunction();
            }
        }
    }

    private void softKeyLeftFunction() {
        if (next) {
            huongDan1 = null;
            sample = null;
            hoverThumb = null;
            nomalThumb = null;
            failThumb = null;
            System.gc();
            midlet.disManager.Home();
        }
        if (question == 1) {
            if (q1.isMenu) {
                if (q1.line == 1) {
                    goiY = true;
                    Alert goiy = new Alert("Gợi ý", (String) imageInfo.getVsuggest(q1.i),
                            null, AlertType.INFO);
                    goiy.setTimeout(Alert.FOREVER);
                    midlet.getDis().setCurrent(goiy, this);
                } else if (q1.line == 2) {
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(DrawChoice.alertCon);
                }
            }
            //xem xet cau tra loi
            if (q1.isAnswer) {
                checkAnswer();
            } else {
                q1.isMenu = !q1.isMenu;
                if (q1.isMenu) {
                    q1.isAnswer = false;
                }
                q1.line = 1;
                repaint();
            }
        } else {
            isMenu = !isMenu;
            line = 1;
            repaint();
        }
    }

    private void softKeyRightFunction() {
        if (next) {
            if (DrawChoice.demBai == 2) {
                huongDan1 = null;
                sample = null;
                hoverThumb = null;
                nomalThumb = null;
                failThumb = null;
                System.gc();
                midlet.disManager.pushDisplayable(new Level2(midlet, imageInfo));
            } else {
                huongDan1 = null;
                sample = null;
                hoverThumb = null;
                nomalThumb = null;
                failThumb = null;
                System.gc();
                midlet.disManager.pushDisplayable(new DrawImage(midlet, imageInfo));
            }
        } else {

            if (question == 1) {
                if (!q1.isMenu) {
                    q1.isAnswer = !q1.isAnswer;
                } else {
                    q1.isMenu = !q1.isMenu;
                }
                if (q1.isAnswer) {
                    q1.isMenu = false;
                }
                q1.line = 1;
                repaint();
            } else {
                if (!isMenu) {
                    if (((x == 1) & (y == 1) & oneOpen) || ((x == 2) & (y == 1) & twoOpen)
                            || ((x == 1) & (y == 2) & threeOpen) || ((x == 2) & (y == 2) & fourOpen)) {
                        question = 1;
                        q1.createQuestion(60);
                        repaint();
                    }
                    if (one && two && three && four && !isMenu) {
                        if (DrawChoice.demBai == 2) {
                            midlet.disManager.pushDisplayable(new Level2(midlet, imageInfo));
                        } else {
                            midlet.disManager.pushDisplayable(new DrawImage(midlet, imageInfo));
                        }
                    }
                }
            }
            if (isMenu) {
                //Menu Huong dan
                if (line == 2) {
                    isMenu = false;
                    line = 1;
                    question = 0;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(huongDan1);
                } //Menu Giai hinh lon
                else if (line == 1) {
                    if ((one & !oneFailed) || (two & !twoFailed) || (three & !threeFailed) || (four & !fourFailed)) {
                        isMenu = false;
                        line = 1;
                        DrawChoice.bigAnswer = true;
                        question = 0;
                        midlet.disManager.pushDisplayable(this);
                        InsertAnswer ia = new InsertAnswer(midlet);
                        midlet.disManager.pushDisplayable(ia.insertAnswer("Giải hình lớn"));
                    }
                } //Menu Luu diem
                else if (line == 3) {
                    isMenu = false;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(new ScoreSave(midlet, false, DrawChoice.score));
                } else if (line == 4) {
                    DrawChoice.score = 0;
                    DrawChoice.liv = 3;
                    DrawChoice.demBai = 0;
                    sample = null;
                    hoverThumb = null;
                    nomalThumb = null;
                    failThumb = null;
                    huongDan1 = null;
                    System.gc();

                    midlet.disManager.Home();
                }
            }
        }
    }

    private void keyUpFunction() {
        if (question == 0) {
            line--;
            if (line == 0) {
                line = 4;
            }
            repaint();
            if (y == 2 && !isMenu) {
                y = 1;
                repaint();
            }
        } else {
            if (q1.isMenu) {
                q1.line--;
                if (q1.line == 0) {
                    q1.line = 2;
                }
                repaint();
            } else if (q1.isAnswer) {
                q1.aline--;
                if (q1.aline == 0) {
                    q1.aline = 4;
                }
                repaint();
            } else if (q1.trans != 0) {
                if (q1.trans < q1.f.getHeight() + 4 - q1.y) {
                    q1.change = false;
                    q1.trans += 5;
                    if (q1.trans == 0) {
                        q1.trans = 1;
                    }
                    repaint();
                }
            }
        }
    }

    private void keyDownFunction() {
        if (question == 0) {
            line++;
            if (line == 5) {
                line = 1;
            }
            repaint();
            if (y == 1 && !isMenu) {
                y = 2;
                repaint();
            }
        } else {
            if (q1.isMenu) {
                q1.line++;
                if (q1.line == 3) {
                    q1.line = 1;
                }
                repaint();
            } else if (q1.isAnswer) {
                q1.aline++;
                if (q1.aline == 5) {
                    q1.aline = 1;
                }
                repaint();
            } else if (q1.trans != 0) {
                q1.change = false;
                q1.trans -= 5;
                if (q1.trans == 0) {
                    q1.trans = 1;
                }
                repaint();
            }
        }
    }

    private void keyCenterFunction() {
        if (question == 0) {
            if (!isMenu) {
                if (((x == 1) & (y == 1) & oneOpen) || ((x == 2) & (y == 1) & twoOpen)
                        || ((x == 1) & (y == 2) & threeOpen) || ((x == 2) & (y == 2) & fourOpen)) {
                    question = 1;
                    q1.createQuestion(60);
                    repaint();
                }
            } else {
                //Menu Huong dan
                if (line == 2) {
                    isMenu = false;
                    line = 1;
                    question = 0;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(huongDan1);
                } //Menu Giai hinh lon
                else if (line == 1) {
                    if ((one & !oneFailed) || (two & !twoFailed) || (three & !threeFailed) || (four & !fourFailed)) {
                        isMenu = false;
                        line = 1;
                        DrawChoice.bigAnswer = true;
                        question = 0;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(DrawTrialLevel.ia.insertAnswer("Giải hình lớn"));
                    }
                } //Menu Luu diem
                else if (line == 3) {
                    isMenu = false;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(new ScoreSave(midlet, false, DrawChoice.score));
                } else if (line == 4) {
                    DrawChoice.score = 0;
                    DrawChoice.liv = 3;
                    DrawChoice.demBai = 0;
                    huongDan1 = null;
                    sample = null;
                    hoverThumb = null;
                    nomalThumb = null;
                    failThumb = null;
                    DrawTrialLevel.ia = null;
                    System.gc();

                    midlet.disManager.Home();
                }
            }
        } else {
            if (q1.isAnswer) {
                checkAnswer();
            }
            if (q1.isMenu) {
                if (q1.line == 1) {
                    q1.isMenu = false;
                    goiY = true;
                    Alert goiy = new Alert("Gợi ý", (String) imageInfo.getVsuggest(q1.i),
                            null, AlertType.INFO);
                    goiy.setTimeout(Alert.FOREVER);
                    midlet.getDis().setCurrent(goiy, this);
                } else if (q1.line == 2) {
                    q1.isMenu = false;
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(DrawChoice.alertCon);
                }
            }
        }
        if (next) {
            if (DrawChoice.demBai == 2) {
                huongDan1 = null;
                sample = null;
                hoverThumb = null;
                nomalThumb = null;
                failThumb = null;
                System.gc();
                midlet.disManager.pushDisplayable(new Level2(midlet, imageInfo));
            } else {
                huongDan1 = null;
                sample = null;
                hoverThumb = null;
                nomalThumb = null;
                failThumb = null;
                System.gc();
                midlet.disManager.pushDisplayable(new DrawImage(midlet, imageInfo));
            }
        }
    }

    protected void keyPressed(int keyCode) {

        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);

        switch (instanceKeyCode) {
            case KeyCodeAdapter.KEY_1:
            case KeyCodeAdapter.SOFT_KEY_LEFT:
                softKeyLeftFunction();
                break;

            case KeyCodeAdapter.KEY_3:
            case KeyCodeAdapter.SOFT_KEY_RIGHT:
                softKeyRightFunction();
                break;

            case KeyCodeAdapter.KEY_2:
            case KeyCodeAdapter.UP_KEY:
                keyUpFunction();
                break;

            case KeyCodeAdapter.KEY_8:
            case KeyCodeAdapter.DOWN_KEY:
                keyDownFunction();
                break;

            case KeyCodeAdapter.KEY_6:
            case KeyCodeAdapter.RIGHT_KEY:
                repaint();
                if (x == 1 && !isMenu && question == 0) {
                    x = 2;
                    repaint();
                }
                break;

            case KeyCodeAdapter.KEY_4:
            case KeyCodeAdapter.LEFT_KEY:
                repaint();
                if (x == 2 && !isMenu && question == 0) {
                    x = 1;
                    repaint();
                }
                break;

            case KeyCodeAdapter.KEY_5:
            case KeyCodeAdapter.CENTER_KEY:
                keyCenterFunction();
                break;
        }
    }

    public static int getScore() {
        return DrawChoice.score;
    }

    public Start getMidlet() {
        return midlet;
    }
}
